﻿var scrollables;

function startNextScrollable() {
    if ($(scrollables).size() <= 0) return;
    scr = scrollables.first();
    scrollables = scrollables.slice(1);
    scr.autoscroll(3000 + Math.floor(Math.random() * 3000));
    if (scrollables.size() > 0) {
        setTimeout(startNextScrollable, Math.floor(Math.random() * 500));
    }
}



//////////////////////////////////////////////
// ANIMAZIONI PER L'APPARIZIONE DELLE TILES
function slideAppear(elements, index, duration, margin) {

	//$(elements).eq(index).animate({ marginLeft: 0, marginRight: margin, opacity: 1 }, duration);
	$(elements).eq(index).animate({opacity: 1 }, duration);
	
    setTimeout(function () {
        if (index < $(elements).size()) slideAppear(elements, index + 1, duration, margin);
    }, duration-200);
}
function slideEnlarge(elements, index, duration, margin) {
    //$(elements).eq(index).show(duration).animate({ marginLeft: 0, marginRight: margin }, duration);
    $(elements).eq(index).show(duration).animate({}, duration);
	
    setTimeout(function () {
        if (index < $(elements).size()) slideEnlarge(elements, index + 1, duration, margin);
    }, duration-200);
}
//
//////////////////////////////////////////////


function startSlideAppear(elements, duration) {
    if ($(elements).size() <= 0) return;
    var margin = $(elements).first().css('margin-right');
    $(elements).css('opacity', '0');//.css('margin-left', margin).css('margin-right', '0px');
    slideAppear(elements, 0, duration, margin);
}
function startSlideEnlarge(elements, duration) {
    if ($(elements).size() <= 0) return;
    var margin = $(elements).first().css('margin-right');
    $(elements).hide();//.css('margin-left', margin).css('margin-right', '0px')
    slideEnlarge(elements, 0, duration, margin);
}

$(function () {
    (function ($) {
        $.fn.shuffle = function () {
            return this.each(function () {
                var items = $(this).children();
                return (items.length)
	        ? $(this).html($.shuffle(items))
	        : this;
            });
        }

        $.shuffle = function (arr) {
            for (
	      var j, x, i = arr.length; i;
	      j = parseInt(Math.random() * i),
	      x = arr[--i], arr[i] = arr[j], arr[j] = x
	    );
            return arr;
        }
    })(jQuery);

    // mischia i loghi degli sponsor
    homelogos = $.shuffle($("#homelogos > div"));
    $("#homelogos").empty().append(homelogos);

    // gestisce l'animazione di apparizione delle tiles
    startSlideAppear($.shuffle($('#agendaprovvisoria > div.tilebig, #agendaprovvisoria > div.tilecontent > div')), 300);
    startSlideAppear($.shuffle($('ul#speaker li')), 250);
    startSlideEnlarge($('#navtile li, ul.megatiles li, ul.megatilesalti.animated li, .entry'), 400);

    $("#navtile li.scrollable").scrollable({ vertical: true, circular: true, speed: 600 });
    scrollables = $.shuffle($("#navtile li.scrollable"));
    setTimeout(startNextScrollable, Math.floor(Math.random() * 500));
});
